package com.wyh.utils.g3utils;

import com.alibaba.excel.EasyExcel;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;

import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.wyh.domain.vo.g3vo.ExcelInfoVo;
import com.wyh.mapper.g3mapper.G3ChargingMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ExcelUtils {

    @Autowired
    G3ChargingMapper g3ChargingMapper;

    //上传
    public static <T> List<ExcelInfoVo> getExcelModelData(final InputStream inputStream, Class<ExcelInfoVo> clazz) {
        if (null == inputStream) {
            throw new NullPointerException("输入流为空!");
        }
        ExcelReaderBuilder result = EasyExcel.read(inputStream, clazz, null).headRowNumber(2);
        ExcelReaderSheetBuilder sheet1 = result.sheet();
        List<ExcelInfoVo> resultData = sheet1.doReadSync();
        return resultData;


    }


    public static List<ExcelInfoVo> readExcel(InputStream inputStream) {
        List<ExcelInfoVo> dataList = new ArrayList<>();
        try {
            // 将输入流转换为字节数组
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) > -1) {
                baos.write(buffer, 0, len);
            }
            baos.flush();

            // 创建两次输入流，一个用于查找头行，一个用于EasyExcel读取
            InputStream inputStreamForHeader = new ByteArrayInputStream(baos.toByteArray());
            InputStream inputStreamForReading = new ByteArrayInputStream(baos.toByteArray());

            int headerRowNum = findHeaderRow(inputStreamForHeader);
            if (headerRowNum != -1) {
                // 必须从零开始计数，所以headerRowNum+1
                EasyExcel.read(inputStreamForReading, ExcelInfoVo.class, new PageReadListener<ExcelInfoVo>(dataList::addAll)).sheet().headRowNumber(headerRowNum + 1).doRead();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataList;
    }

    private static int findHeaderRow(InputStream inputStream) {
        try (Workbook workbook = new XSSFWorkbook(inputStream)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (int i = 0; i < 3; i++) {  // 假设列名在前3行之一
                Row row = sheet.getRow(i);
                if (row != null) {
                    for (Cell cell : row) {
                        if ("组织名称".equals(cell.getStringCellValue())) {
                            return i;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;  // 未找到列名
    }



    public static boolean getOneOrTwo(boolean b){
        return b;
    }










    public static List<ExcelInfoVo> judgeError(List<ExcelInfoVo> list) {

        for (ExcelInfoVo excelDatum : list) {

            if (StringUtils.isAnyBlank(excelDatum.getOrganizationName(), excelDatum.getPropertyname(), excelDatum.getBuildingname(),
                    excelDatum.getResourcename(), excelDatum.getCustomerName(), excelDatum.getBillingitemsname())
                    || excelDatum.getStarttime() == null || excelDatum.getEndtime() == null || excelDatum.getBillingtime() == null
                    || excelDatum.getPrice() == null || excelDatum.getReceivable() == null || excelDatum.getFloorname() == null && excelDatum.getUnitname() == null) {

                excelDatum.setErrorReason("该行有必填字段为空");
            }

        }

        return list;
    }

    public static boolean judgeErrorTorF(List<ExcelInfoVo> list) {
        for (ExcelInfoVo excelDatum : list) {

            if (StringUtils.isAnyBlank(excelDatum.getOrganizationName(), excelDatum.getPropertyname(), excelDatum.getBuildingname(),
                    excelDatum.getResourcename(), excelDatum.getCustomerName(), excelDatum.getBillingitemsname())
                    || excelDatum.getStarttime() == null || excelDatum.getEndtime() == null || excelDatum.getBillingtime() == null
                    || excelDatum.getPrice() == null || excelDatum.getReceivable() == null || excelDatum.getFloorname() == null && excelDatum.getUnitname() == null) {
                return false;


            }

        }
        return true;

    }


    //判断园区是否在该组织下
    public int findPId(ExcelInfoVo excelInfoVo, int id) {
        int pId = g3ChargingMapper.findPId(excelInfoVo.getParkName(), id);
        return pId;
    }


}

